package view;



import Dao.RalationalDataDao;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Vector;

public class RelationFra extends JInternalFrame{
    private JPanel Panel;
    private JTable table;
    private JButton UpdateBtn;
    private JButton deleteBtn;
    private JButton addBtn;
    private JScrollPane SPanel;

    String tableName;
    Connection conn;

    RalationalDataDao dbsDao=new RalationalDataDao();
    ArrayList<String> columnName=new ArrayList<>();
    ArrayList<String> columnType=new ArrayList<>();

    RelationFra(Connection conn,String tableName){
        setContentPane(Panel);
        setVisible(true);
        setBounds(50,20,650,500);
        setDefaultCloseOperation(DISPOSE_ON_CLOSE);

        this.conn=conn;
        this.tableName=tableName;
//        SPanel.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
        //初始化表格
        InitialTable();
        //创建监听
        addLisner();
    }

    private void addLisner() {
        //删除
        deleteBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                delActPerformed(e);
            }
        });
        //修改
        UpdateBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                UpdateActPerf();
            }
        });
        addBtn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addActPerformed();
            }
        });
    }

    //添加按钮响应函数
    private void addActPerformed() {
        new AddRltFra(conn,this,tableName,columnName,columnType);
    }

    //修改按钮响应函数
    private void UpdateActPerf() {
        int row=table.getSelectedRow();
        if(row==-1){
            JOptionPane.showMessageDialog(null,"请选择信息");
            return;
        }
        //获取数据
        ArrayList<String> data=new ArrayList<>();
        for (int i = 0; i < columnName.size(); i++) {
            String s=(String) table.getValueAt(row,i);
            if(s!=null)
                data.add((String) table.getValueAt(row,i));
            else
                data.add("");
        }
        //新建窗口
        new UpdateRltFra(conn,this,tableName,columnName,columnType,data);
    }

    //删除响应函数
    private void delActPerformed(ActionEvent e) {
        int row= table.getSelectedRow();
        if(row==-1){
            JOptionPane.showMessageDialog(null,"请选择要删除的信息");
            return;
        }
        int t=JOptionPane.showConfirmDialog(null,"您确定要永久的删除此信息吗");
        if(t!=0){
            return;
        }

        int id=Integer.parseInt((String)table.getValueAt(row,0));
        try {
            //删除
            int numOfaff=dbsDao.delData(conn,tableName,id);
            if(numOfaff>0){
                JOptionPane.showMessageDialog(null,"删除成功");
                fillTable();
            }
            else {
                JOptionPane.showMessageDialog(null,"删除失败");
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null,"数据库异常");
            throw new RuntimeException(ex);
        }
    }

    //初始化表格
    private void InitialTable() {
        try {
            //获取列名和数据类型
            ResultSet set=dbsDao.getColumnsName(conn,tableName);
            while (set.next()){
                columnName.add(set.getString(1));
                columnType.add(set.getString(2));
            }

            String [] s_columnName = new String[columnName.size()];
            columnName.toArray(s_columnName);
            String [][] data={};
            DefaultTableModel defaultTableModel=new DefaultTableModel(data,s_columnName);
            table.setModel(defaultTableModel);

            fillTable();//填充表格
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"数据库异常");
            throw new RuntimeException(e);
        }
    }

    //填充表格内容
    public void fillTable() {
        DefaultTableModel model= (DefaultTableModel) table.getModel();
        model.setRowCount(0);//清空
        try {
            ResultSet set=dbsDao.listData(conn,tableName);//获取数据
            while (set.next()){
                Vector<String> v=new Vector<String>();
                for (String s : columnName) {
                    v.add(set.getString(s));
                }
                model.addRow(v);
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null,"数据库异常");
            throw new RuntimeException(e);
        }
    }
}
